package com.jiutong.meipenggang.ui.activitys;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.ui.fragments.HomeFragment;
import com.jiutong.meipenggang.ui.fragments.InvestFragment;
import com.jiutong.meipenggang.ui.fragments.MeiCaiBaoFragment;
import com.jiutong.meipenggang.ui.fragments.MineFragment;
import com.jiutong.meipenggang.utils.SpConfig;


public class MainActivity extends BaseActivity implements View.OnClickListener {

    private Fragment mContent;
    private HomeFragment homeFragment;
    private MeiCaiBaoFragment meiCaiBaoFragment;
    private RadioButton rb_home;
    private RadioButton rb_mine;
    private RadioButton rb_discover;
    private RadioButton rb_manager_money;
    private MineFragment mineFragment;
    private InvestFragment investFragment;
    private long currentTime;
    //    private View vcant;
    private RadioGroup rg_main_tab;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_main;
    }


    @Override
    protected void initView() {
        mImmersionBar.fitsSystemWindows(false).statusBarDarkFont(false).init();
        rb_home = (RadioButton) findViewById(R.id.rb_home);
        rb_manager_money = (RadioButton) findViewById(R.id.rb_manager_money);
        rb_mine = (RadioButton) findViewById(R.id.rb_mine);
        rb_discover = (RadioButton) findViewById(R.id.rb_discover);
//        vcant = findViewById(R.id.v_cant);
        rg_main_tab = (RadioGroup) findViewById(R.id.main_tab_group);
//        if (SpConfig.getInstance().getBool(Constants.IS_LOGIN)) {
//            vcant.setVisibility(View.GONE);
//            enableRadioGroup(rg_main_tab);
//        } else {
//            disableRadioGroup(rg_main_tab);
//            vcant.setVisibility(View.VISIBLE);
//        }

        meiCaiBaoFragment = new MeiCaiBaoFragment();
        homeFragment = new HomeFragment();
        mineFragment = new MineFragment();
        investFragment = new InvestFragment();

        rb_home.setChecked(true);
        commitFragment(R.id.main_frameLayout, homeFragment);
        mContent = homeFragment;

    }

    public void disableRadioGroup(RadioGroup testRadioGroup) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(false);
        }
    }

    public void enableRadioGroup(RadioGroup testRadioGroup) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(true);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if (SpConfig.getInstance().getBool(Constants.IS_LOGIN)) {
//            vcant.setVisibility(View.GONE);
//            enableRadioGroup(rg_main_tab);
//        } else {
//            disableRadioGroup(rg_main_tab);
//            vcant.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.keyboardEnable(true).init();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        rb_home.setOnClickListener(this);
        rb_manager_money.setOnClickListener(this);
        rb_mine.setOnClickListener(this);
        rb_discover.setOnClickListener(this);
    }


    public void commitFragment(int layoutId, Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(layoutId, fragment);
        transaction.commit();
    }

    /**
     * 切换tab页面
     *
     * @param fragment 要显示的fragment
     */
    public void switchContent(Fragment fragment) {
        if (mContent != fragment) {
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            if (!fragment.isAdded()) {
                transaction.hide(mContent).add(R.id.main_frameLayout, fragment)
                        .commit();
            } else {
                transaction.hide(mContent).show(fragment)
                        .commitAllowingStateLoss();
            }
            mContent = fragment;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_home:
                switchContent(homeFragment);
                mImmersionBar.statusBarDarkFont(false).init();
                break;
            case R.id.rb_manager_money:
                switchContent(meiCaiBaoFragment);
                mImmersionBar.fitsSystemWindows(false).statusBarDarkFont(false).init();
                break;
            case R.id.rb_mine:
                switchContent(mineFragment);
                mImmersionBar.fitsSystemWindows(false).statusBarDarkFont(false).init();
                break;
            case R.id.rb_discover:
                switchContent(investFragment);
                mImmersionBar.fitsSystemWindows(false).statusBarDarkFont(false).init();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {

            if (System.currentTimeMillis() - currentTime <= 3000) {
//                AppManager.getAppManager().finishAllActivity();
                SpConfig.getInstance().edit().putString(Constants.TEPPHONE_STRING, "").commit();
                SpConfig.getInstance().edit().putBoolean(Constants.IS_LOGIN, false).commit();
                SpConfig.getInstance().edit().putBoolean(Constants.CHANGE_SHOUSHI, false).commit();
                SpConfig.getInstance().edit().putString(Constants.TOKEN, "").commit();
                SpConfig.getInstance().edit().putString(Constants.ID, "").commit();
                SpConfig.getInstance().edit().putBoolean(Constants.TOKEN_FAIL, false).commit();
                AppManager.getAppManager().AppExit(this);
//                return super.onKeyDown(keyCode, event);
            } else {
                toastShortShow("请再次点击退出应用");
                currentTime = System.currentTimeMillis();
//                return false;
            }
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

}
